package com.ama.controller;

import com.ama.pojo.Company;
import com.ama.pojo.Person;
import com.ama.service.CompanyService;
import com.ama.service.PersonService;
import com.ama.utils.SpringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class UpdateController {
    @RequestMapping("/updateInfo")
    public void updateInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Person person = (Person) session.getAttribute("person");
        Company company = (Company) session.getAttribute("company");
        String name = request.getParameter("name");
        String school = request.getParameter("school");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String trade = request.getParameter("trade");
        String salary = request.getParameter("salary");
        String tip = request.getParameter("tip");
        String type = request.getParameter("type");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<script>");
        if("个人".equals(type)){
            String sex = request.getParameter("sex");
            String birthday = request.getParameter("birthday");
            Person new_person = new Person(
                    person.getId(),person.getUsername(),person.getPassword(),name, sex, birthday, phone, email, school, trade, salary, tip,person.getPubtime());
            SpringUtils.getBean(PersonService.class).update(person);
            session.setAttribute("person", new_person);
            out.println("alert('修改成功')");
            out.println("window.open ('"+request.getContextPath()+"/index.jsp','_top')");
        }
        if("公司".equals(type)){
            String location = request.getParameter("location");
            Company new_company = new Company(
                    company.getId(),company.getUsername(),company.getPassword(), name, phone, email, location, trade, salary, tip,company.getPubtime());
            SpringUtils.getBean(CompanyService.class).update(new_company);
            session.setAttribute("company", new_company);
            out.println("alert('修改成功')");
            out.println("window.open ('"+request.getContextPath()+"/c_index.jsp','_top')");
        }
        out.println("</script>");
        out.println("</html>");
        out.flush();
        out.close();
    }
}
